package com.cetcs.kmga.dataManager.controller;


import com.cetc.cloud.kmga.util.DateUtils;
import com.cetcs.kmga.common.*;
import com.cetcs.kmga.dataManager.annotation.LogAnnotation;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.CircularChartVo;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.FlipHistogramChartVo;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.MultiLineChartVo;
import com.cetcs.kmga.dataManager.entity.global.LogLevelEnum;
import com.cetcs.kmga.dataManager.entity.global.LogTypeEnum;
import com.cetcs.kmga.dataManager.service.dataManager.RiskEventService;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 数据监管.风险事件页面控制器
 */
@RestController
@RequestMapping("/riskEvent")
public class RiskEventController extends BaseController {

    @Autowired
    private RiskEventService riskEventService;

    /**
     * 风险事件统计
     *
     * @return
     */
    @LogAnnotation(module = "数据资源管理-风险事件-风险概览", logType = LogTypeEnum.业务日志, description = "风险事件统计", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findEventStatistics", method = RequestMethod.GET)
    public BaseResponse findEventStatistics(@RequestParam(value = "startTime", required = false) String startDate, @RequestParam(value = "endTime", required = false) String endDate) {
        try {
            //checkTimeInput(startDate, endDate);
            //获取当前时间和前一个月的时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date time = new Date();
            Calendar calendar = Calendar.getInstance();
            //获取开始时间
            calendar.setTime(time);
            List<String> xData = Lists.newArrayList();
            calendar.add(Calendar.MONTH, -3);
            startDate = sdf.format(calendar.getTime()).substring(0,10);
            while (!sdf.format(time).substring(0,10).equals(sdf.format(calendar.getTime()).substring(0,10))){
                //如果计算后的日期和当前的日期相同，跳出循环
                calendar.add(Calendar.DATE, 1);
                xData.add(sdf.format(calendar.getTime()).substring(0,10));
            }
            endDate = sdf.format(time).substring(0,10);
            MultiLineChartVo result = riskEventService.findEventStatistics(startDate, endDate,xData);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 风险级别分布统计
     *
     * @return
     */
    @LogAnnotation(module = "数据资源管理-风险事件-风险概览", logType = LogTypeEnum.业务日志, description = "风险级别分布统计", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findRiskLevelStatistic", method = RequestMethod.GET)
    public BaseResponse findRiskLevelStatistic(@RequestParam(value = "startTime", required = false) String startDate,
                                               @RequestParam(value = "endTime", required = false) String endDate) {
        try {
            if(startDate==null || "".equals(startDate.trim())) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date time = new Date();
                Calendar calendar = Calendar.getInstance();
                //获取开始时间
                calendar.setTime(time);
                List<String> xData = Lists.newArrayList();
                calendar.add(Calendar.MONTH, -3);
                startDate = sdf.format(calendar.getTime());
                endDate = sdf.format(time);
            }else{
                startDate = startDate + " 00:00:00";
                endDate = endDate + " 23:59:59";
            }
            checkTimeInput(startDate, endDate);
            CircularChartVo ret = riskEventService.findRiskLevelStatistic(startDate, endDate);
            return this.ok(ret);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 风险数据源
     *
     * @return
     */
    @LogAnnotation(module = "数据资源管理-风险事件-风险概览", logType = LogTypeEnum.业务日志, description = "风险数据源分布统计", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findRiskDataSourceStatistic", method = RequestMethod.GET)
    public BaseResponse findRiskDataSourceStatistic(@RequestParam(value = "startTime", required = false) String startDate,
                                                    @RequestParam(value = "endTime", required = false) String endDate,
                                                    @RequestParam(value = "type",required = false)String type,
                                                    @RequestParam(value = "isLimit", defaultValue = "5") Integer isLimit) {
        try {
            if(startDate==null || "".equals(startDate.trim())) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date time = new Date();
                Calendar calendar = Calendar.getInstance();
                //获取开始时间
                calendar.setTime(time);
                List<String> xData = Lists.newArrayList();
                calendar.add(Calendar.MONTH, -3);
                startDate = sdf.format(calendar.getTime());
                endDate = sdf.format(time);
            }else{
                startDate = startDate + " 00:00:00";
                endDate = endDate + " 23:59:59";
            }
            checkTimeInput(startDate, endDate);
            FlipHistogramChartVo result = riskEventService.findRiskDataSourceStatistic(startDate, endDate, type,isLimit);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }






    /**
     * 风险IP
     *
     * @return
     */
    @LogAnnotation(module = "数据资源管理-风险事件-风险概览", logType = LogTypeEnum.业务日志, description = "风险IP分布统计", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findRiskIpStatistic", method = RequestMethod.GET)
    public BaseResponse findRiskIpStatistic(@RequestParam(value = "startTime", required = false) String startDate,
                                            @RequestParam(value = "endTime", required = false) String endDate,
                                            @RequestParam(value = "type",required = false)String type,
                                            @RequestParam(value = "isLimit", defaultValue = "5") Integer isLimit) {
        try {
            if(startDate==null || "".equals(startDate.trim())) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date time = new Date();
                Calendar calendar = Calendar.getInstance();
                //获取开始时间
                calendar.setTime(time);
                List<String> xData = Lists.newArrayList();
                calendar.add(Calendar.MONTH, -3);
                startDate = sdf.format(calendar.getTime());
                endDate = sdf.format(time);
            }else{
                startDate = startDate + " 00:00:00";
                endDate = endDate + " 23:59:59";
            }
            checkTimeInput(startDate, endDate);
            FlipHistogramChartVo result = riskEventService.findRiskIpStatistic(startDate, endDate, type,isLimit);

            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 检查输入的时间格式
     */
    private void checkTimeInput(String startTime, String endTime) {
        checkTimeInputFormat(startTime);
        checkTimeInputFormat(endTime);
    }

    /**
     * 检查字符串是否符合日期格式
     *
     * @param timeStr
     */
    private void checkTimeInputFormat(String timeStr) {
        if (StringUtils.isEmpty(timeStr)) return;
        Date s = DateUtils.parse(timeStr, DateUtils.DATE_FORMAT_1);
        if (s == null) {
            s = DateUtils.parse(timeStr, DateUtils.DATE_FORMAT_2);
            if (s == null) throw new RuntimeException(timeStr + "格式不正确");
        }
    }




    //========================yyangs start======================


    /**
     * 风险事件排名-资源 分页
     *
     * @return
     */
    @LogAnnotation(module = "数据资源管理-风险事件-风险概览", logType = LogTypeEnum.业务日志, description = "风险事件排名资源列表", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findRiskDataSourceListsByPage", method = RequestMethod.GET)
    public BaseResponse findRiskDataSourceListsByPage(WebRequest request,
                                                      @RequestParam(required =false,defaultValue ="1")int page,
                                                      @RequestParam(required =false,defaultValue ="10")int pageSize,
                                                      @RequestParam(required =false,defaultValue = "id") String sort,
                                                      @RequestParam(required =false,defaultValue = "desc") String order,
                                                      @RequestParam(required =false,defaultValue = "1") String type,
                                                      @RequestParam(required =false) String startTime,
                                                      @RequestParam(required =false) String endTime) {
        try {
            Data params = new Data();
            params.put("page",page);
            params.put("pageSize",pageSize);
            params.put("sort",type);
            params.put("order",order);
            if(startTime!=null && !"".equals(startTime)) {
                params.put("startDate", startTime + " 00:00:00");
            }
            if(endTime!=null && !"".equals(endTime )) {
                params.put("endDate", endTime + " 23:59:59");
            }
            Page result = riskEventService.findRiskDataSourceListsByPage(params);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }

    /**
     * 风险事件排名-客户端IP 分页
     *
     * @return
     */
    @LogAnnotation(module = "数据资源管理-风险事件-风险概览", logType = LogTypeEnum.业务日志, description = "风险事件排名客户端IP列表", logLevel = LogLevelEnum.低)
    @RequestMapping(value = "/findRiskIPListsByPage", method = RequestMethod.GET)
    public BaseResponse findRiskIPListsByPage(WebRequest request,
                                              @RequestParam(required =false,defaultValue ="1")int page,
                                              @RequestParam(required =false,defaultValue ="10")int pageSize,
                                              @RequestParam(required =false,defaultValue = "client_ip") String sort,
                                              @RequestParam(required =false,defaultValue = "desc") String order,
                                              @RequestParam(required =false,defaultValue = "1") String type,
                                              @RequestParam(required =false) String startTime,
                                              @RequestParam(required =false) String endTime) {
        try {
            Data params = new Data();
            params.put("page",page);
            params.put("pageSize",pageSize);
            params.put("sort",type);
            params.put("order",order);
            if(startTime!=null && !"".equals(startTime)) {
                params.put("startDate", startTime + " 00:00:00");
            }
            if(endTime!=null && !"".equals(endTime )) {
                params.put("endDate", endTime + " 23:59:59");
            }
            Page result = riskEventService.findRiskIPListsByPage(params);
            return this.ok(result);
        } catch (Exception e) {
            e.printStackTrace();
            return this.error(Constant.SYSTEM_EXCEPTION);
        }
    }


    //========================yyangs end======================

}
